package com.xzkydz.bluetoothlib.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;

public class MyButton extends android.support.v7.widget.AppCompatImageButton {
    private ObjectAnimator objectAnimator;

    public static final int STATE_FRESH =1;//正在播放
    public static final int STATE_STOP =3;//停止
    public int state;
    public MyButton(Context context) {
        super(context);
        init();
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        state = STATE_STOP;
        objectAnimator = ObjectAnimator.ofFloat(this, "rotation", 0f, 360f);//添加旋转动画，旋转中心默认为控件中点
        objectAnimator.setDuration(1000);//设置动画时间
        objectAnimator.setInterpolator(new LinearInterpolator());//动画时间线性渐变
        objectAnimator.setRepeatCount(ObjectAnimator.INFINITE);
        objectAnimator.setRepeatMode(ObjectAnimator.RESTART);
    }

    public void Searching(){
        if(state == STATE_STOP){
            objectAnimator.start();//动画开始
            state = STATE_FRESH;
        }else if(state == STATE_FRESH){
            objectAnimator.end();//动画暂停
            state = STATE_STOP;
        }
    }

    public void Stop(){
        objectAnimator.end();//动画结束
        state = STATE_STOP;
    }
}
